import { setMenus, getMenus } from '@/utils/localStorageData'
import { userMenus } from '@/api/user'
const state = {
  menus: getMenus()
}

const mutations = {
  SET_MENUS: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  userMenus({ commit }) {
    return new Promise((resolve, reject) => {
      userMenus().then(response => {
        if (response.code === 200) {
          const { data } = response
          commit('SET_MENUS', data)
          setMenus(data)
          resolve(data)
        } else {
          reject(response.msg)
        }
      }).catch(error => {
        reject(error)
      })

    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
